home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch15 / Trans2.cls < prev    next >
Text File  |  1999-06-25  |  5KB  |  155 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Transformed3d"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Private NumCurvePts As Integer
  17. Private CurvePoints() As Point3D
  18.  
  19. Private NumTransformations As Integer
  20. Private Transformations() As Transformation
  21.  
  22. Private Faces As Collection
  23. ' Add a point to the curve.
  24. Public Sub AddCurvePoint(ByVal X As Single, ByVal Y As Single, ByVal z As Single)
  25.     NumCurvePts = NumCurvePts + 1
  26.     ReDim Preserve CurvePoints(1 To NumCurvePts)
  27.     With CurvePoints(NumCurvePts)
  28.         .coord(1) = X
  29.         .coord(2) = Y
  30.         .coord(3) = z
  31.         .coord(4) = 1
  32.     End With
  33. End Sub
  34.  
  35.  
  36. ' Set a transformation.
  37. Public Sub SetTransformation(M() As Single)
  38.     NumTransformations = NumTransformations + 1
  39.     ReDim Preserve Transformations(1 To NumTransformations)
  40.     m3MatCopy Transformations(NumTransformations).M, M
  41. End Sub
  42. ' Create the display faces by applying the
  43. ' series of transformations in array M().
  44. Public Sub Transform()
  45. Dim i As Integer
  46. Dim j As Integer
  47. Dim x0 As Single
  48. Dim y0 As Single
  49. Dim z0 As Single
  50. Dim x1 As Single
  51. Dim y1 As Single
  52. Dim z1 As Single
  53. Dim x2 As Single
  54. Dim y2 As Single
  55. Dim z2 As Single
  56.  
  57.     Set ThePolyline = New Polyline3d
  58.  
  59.     ' Add the original curve to ThePolyline.
  60.     x1 = CurvePoints(1).coord(1)
  61.     y1 = CurvePoints(1).coord(2)
  62.     z1 = CurvePoints(1).coord(3)
  63.     For j = 2 To NumCurvePts
  64.         x2 = CurvePoints(j).coord(1)
  65.         y2 = CurvePoints(j).coord(2)
  66.         z2 = CurvePoints(j).coord(3)
  67.         ThePolyline.AddSegment x1, y1, z1, x2, y2, z2
  68.         x1 = x2
  69.         y1 = y2
  70.         z1 = z2
  71.     Next j
  72.  
  73.     ' Start with the transformed coordinates
  74.     ' the same as the original coordinates.
  75.     For j = 1 To NumCurvePts
  76.         CurvePoints(j).trans(1) = CurvePoints(j).coord(1)
  77.         CurvePoints(j).trans(2) = CurvePoints(j).coord(2)
  78.         CurvePoints(j).trans(3) = CurvePoints(j).coord(3)
  79.     Next j
  80.  
  81.     ' Create the transformed copies of the curve.
  82.     For i = 1 To NumTransformations
  83.         ' Place the first point.
  84.         x1 = CurvePoints(1).trans(1)
  85.         y1 = CurvePoints(1).trans(2)
  86.         z1 = CurvePoints(1).trans(3)
  87.         m3ApplyFull _
  88.             CurvePoints(1).coord, _
  89.             Transformations(i).M, _
  90.             CurvePoints(1).trans
  91.         x0 = CurvePoints(1).trans(1)
  92.         y0 = CurvePoints(1).trans(2)
  93.         z0 = CurvePoints(1).trans(3)
  94.         ThePolyline.AddSegment x1, y1, z1, x0, y0, z0
  95.  
  96.         ' Add the rest of the points.
  97.         For j = 2 To NumCurvePts
  98.             x1 = CurvePoints(j).trans(1)
  99.             y1 = CurvePoints(j).trans(2)
  100.             z1 = CurvePoints(j).trans(3)
  101.             m3ApplyFull _
  102.                 CurvePoints(j).coord, _
  103.                 Transformations(i).M, _
  104.                 CurvePoints(j).trans
  105.             x2 = CurvePoints(j).trans(1)
  106.             y2 = CurvePoints(j).trans(2)
  107.             z2 = CurvePoints(j).trans(3)
  108.             ' (x0, y0, z0) = previous point, new.
  109.             ' (x1, y1, z1) = current point, old.
  110.             ' (x2, y2, z2) = current point, new.
  111.             ThePolyline.AddSegment x0, y0, z0, x2, y2, z2
  112.             ThePolyline.AddSegment x1, y1, z1, x2, y2, z2
  113.             x0 = x2
  114.             y0 = y2
  115.             z0 = z2
  116.         Next j
  117.     Next i
  118. End Sub
  119. ' Apply a transformation matrix which may not
  120. ' contain 0, 0, 0, 1 in the last column to the
  121. ' object.
  122. Public Sub ApplyFull(M() As Single)
  123. Dim i As Integer
  124.  
  125.     ' Transform the curve.
  126.     For i = 1 To NumCurvePts
  127.         m3ApplyFull CurvePoints(i).coord, M, _
  128.                     CurvePoints(i).trans
  129.     Next i
  130.     
  131.     ' Transform the display polyline if it exists.
  132.     If Not ThePolyline Is Nothing Then ThePolyline.ApplyFull M
  133. End Sub
  134.  
  135. ' Apply a transformation matrix to the object.
  136. Public Sub Apply(M() As Single)
  137. Dim i As Integer
  138.  
  139.     ' Transform the curve.
  140.     For i = 1 To NumCurvePts
  141.         m3Apply CurvePoints(i).coord, M, _
  142.                 CurvePoints(i).trans
  143.     Next i
  144.     
  145.     ' Transform the display polyline if it exists.
  146.     If Not ThePolyline Is Nothing Then ThePolyline.Apply M
  147. End Sub
  148.  
  149.  
  150. ' Draw the extrusion on a PictureBox.
  151. Public Sub Draw(ByVal pic As PictureBox, Optional r As Variant)
  152.     If Not ThePolyline Is Nothing Then _
  153.         ThePolyline.Draw pic, r
  154. End Sub
  155.